//
//  HLTextView.m
//  OrderSys
//
//  Created by 1 on 2017/7/6.
//  Copyright © 2017年 com.hx. All rights reserved.
//

#import "HLTextView.h"

@implementation HLTextView

- (void)dealloc
{
     NSLog(@"%@ 这个类被强暴了",NSStringFromClass([self class]));
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//- (id)initWithFrame:(CGRect)frame
//{
//    if ((self = [super initWithFrame:frame])) {
//         [self _init];
//    }
//    return self;
//}

-(instancetype)init
{
    if ((self = [super init])) {
        
        [self _init];
    }
    return self;
}

-(void)_init
{
    self.placeholder = @"";
    
    self.placeholderX =8;
    
    self.textLength = 0;
    
    self.isCHECKLENGTH = NO;
    
    float  screenW = [[UIScreen mainScreen] bounds].size.width;
    CGFloat kWratio  = screenW / 375.0;
    self.font = [UIFont systemFontOfSize:kWratio*13];
    
    self.placeholderColor = [UIColor lightGrayColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
    
}



- (void)textChanged:(NSNotification *)notification
{
    if (self.placeholder.length == 0)
        return;
    
    if (self.text.length == 0) {
        [[self viewWithTag:999] setAlpha:1];
    } else {
        [[self viewWithTag:999] setAlpha:0];
    }
}





- (void)setText:(NSString *)text
{
    [super setText:text];
    
    [self textChanged:nil];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if (self.isCHECKLENGTH ==NO) {
        return YES;
    }else
    {
        if ([text isEqualToString:@""] && range.length > 0) {
            //删除字符肯定是安全的
            return YES;
        }
        else {
            if (textView.text.length - range.length + text.length >self.textLength) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"超出最大可输入长度" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alert show];
                return NO;
            }
            else {
                return YES;
            }
        }
    }
}


- (void)drawRect:(CGRect)rect
{
    float  screenH = [[UIScreen mainScreen] bounds].size.height;
    
    float  screenW = [[UIScreen mainScreen] bounds].size.width;
    float kWratio  =screenW / 375.0;
    float kHratio =screenH / 667.0;
    if (self.placeholder.length > 0) {
        if (!_placeholderLabel) {
            _placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.placeholderX, 8, self.bounds.size.width - 2*self.placeholderX, 0)];
            _placeholderLabel.lineBreakMode = NSLineBreakByWordWrapping;
            _placeholderLabel.numberOfLines = 0;
            _placeholderLabel.font = self.font;
            _placeholderLabel.backgroundColor = [UIColor clearColor];
            _placeholderLabel.textColor = self.placeholderColor;
            _placeholderLabel.alpha = 0;
            _placeholderLabel.tag = 999;
            [self addSubview:_placeholderLabel];
        }
        
        _placeholderLabel.textAlignment = self.textAlignment;
        _placeholderLabel.text = self.placeholder;
        [_placeholderLabel sizeToFit];
        CGRect frame = _placeholderLabel.frame;
        frame.size.width = self.bounds.size.width - 4.5* 2;//self.frame.origin.x * 2;
        _placeholderLabel.frame = frame;
        [self sendSubviewToBack:_placeholderLabel];
    }
    
    if (self.isCHECKLENGTH==YES) {
        if (!_labelLength) {
            _labelLength = [[UILabel alloc] init];
            _labelLength.numberOfLines = 1;
            _labelLength.font = self.font;
            _labelLength.backgroundColor = [UIColor clearColor];
            _labelLength.text = [NSString stringWithFormat:@"0/%f",self.textLength];
            _labelLength.textColor = [UIColor blackColor];
            
            _labelLength.frame =CGRectMake(self.bounds.size.width-kWratio *100,self.bounds.size.height-kHratio*20,0,0);
            [_labelLength sizeToFit];
            _labelLength.tag = 500;
            [self addSubview:_labelLength];
        }
    }
    
    if (self.text.length == 0 && self.placeholder.length > 0) {
        [[self viewWithTag:999] setAlpha:1];
    }
    
    [super drawRect:rect];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
